package com.example.demo.controller;

import com.example.demo.entity.StudentInfo;
import com.example.demo.service.StudentInfoService;
import com.example.demo.vo.StudentInfoClassInfo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


//跨域请求注解
@CrossOrigin

@RestController
@RequestMapping("stu")
public class StudentInfoController {
    @Autowired
    private StudentInfoService studentInfoService;

    //http://localhost:8080/stu/query  get  name
    @GetMapping("/query")
    public List<StudentInfo> queryStudentInfoByName(String name) {
        return studentInfoService.getAllStudentInfo(name);
    }
  @GetMapping("querystu")
    public List<StudentInfoClassInfo> queryStudentInfoAndClassInfoByName(@RequestParam String name,@RequestParam Integer clsid) {
        List<StudentInfoClassInfo>stuClass = null;
        if(clsid==0){//当clsid为零，表示返回所有学生信息或根据姓名查询来返回学生信息
            stuClass =studentInfoService.getAllStuAndClassByName(name);
        }else{//当clsid不为0，表示根据班级查询学生信息或根据班级和姓名查询学生信息
            stuClass = studentInfoService.getAllStuAndClassByclsAndName(name,clsid);
        }

      return stuClass;
  }

  //http://localhost:8080/stu/del/197
  @GetMapping("del/{id}")
  private Integer delStudentInfo(@PathVariable Integer id) {
        return studentInfoService.deleteByID(id);
  }
    //http://localhost8080/stu/add
  @PostMapping("add")
    public Integer addStudentInfo(@RequestBody StudentInfo studentInfo) {
        Integer i = studentInfoService.insertStudentInfo(studentInfo);
        return i;
  }

  //http://localhost:8080/stu/update
  @PostMapping("update")
    public Integer updateStudentInfo(@RequestBody StudentInfo studentInfo) {
        Integer i = studentInfoService.updateStudentInfo(studentInfo);
        return i;
  }

  @GetMapping("page")
    public PageInfo<StudentInfoClassInfo> pageStudentInfoClassInfo(@RequestParam Integer page,@RequestParam Integer size) {
        return studentInfoService.getPageStudentInfoClassInfo(page,size);


  }

}
